local Version = "0.4";
local Author = "Delak"

expression = "0";
lastCommand = "num";
lastOp = "";
lastDivIdx = 0;
dotPunched = false;
memory = 0;

function guiCalc_OnLoad()

	SLASH_guiCalc1 = "/guiCalc";
	SLASH_guiCalc2 = "/gc";
	SlashCmdList["guiCalc"] = function (editbox, msg) 
		guiCalc_Toggle();
	end
end

function guiCalc_Toggle(num)

	if (guiCalcFrame) then
		if ( guiCalcFrame:IsVisible() ) then
			guiCalcFrame:Hide()
		else
			guiCalcFrame:Show()
		end
	end
end

function guiCalc_OnMouseDown(this)
	this:StartMoving("TOPLEFT");
end;

function guiCalc_OnMouseUp(this)
	this:StopMovingOrSizing();
end;

function ButtonDel_OnClick()
	if ( string.len(expression) ~= 0 ) then
		lastChar = string.sub(expression,string.len(expression),string.len(expression));
		preLastChar = "";
		if (string.len(expression) > 1 ) then
			preLastChar = string.sub(expression,string.len(expression)-1,string.len(expression)-1);
		end
		expression = string.sub(expression,1,string.len(expression)-1);
		if ( lastChar == '.' ) then
			dotPunched = false;
		end
		if ( preLastChar == '+' or preLastChar == '-' or preLastChar == '/' or preLastChar == '*' ) then
			lastCommand = "op"
		end	
	end
	if ( string.len(expression) == 0 ) then
		reset();
	end
	updateVisor(expression);
end

function Button1_OnClick()
	numClick("1");
end

function Button2_OnClick()
	numClick("2");
end

function Button3_OnClick()
	numClick("3");
end

function Button4_OnClick()
	numClick("4");
end

function Button5_OnClick()
	numClick("5");
end

function Button6_OnClick()
	numClick("6");
end

function Button7_OnClick()
	numClick("7");
end

function Button8_OnClick()
	numClick("8");
end

function Button9_OnClick()
	numClick("9");
end

function Button0_OnClick()
	numClick("0");
end

function ButtonAdd_OnClick()
	opClick("+");
end

function ButtonSub_OnClick()
	opClick("-");
end

function ButtonMult_OnClick()
	opClick("*");
end

function ButtonDiv_OnClick()
	opClick("/");
end

function ButtonMSave_OnClick()
	memory = Text_calc:GetText();
end

function ButtonMRecall_OnClick()
	Text_calc:SetText(memory);
end

function ButtonMAdd_OnClick()
	expression = expression + memory;
	Text_calc:SetText(expression);
end

function ButtonMSub_OnClick()
	expression = expression - memory;
	Text_calc:SetText(expression);
end

function ButtonMClear_OnClick()
	memory = 0;
end

function ButtonDot_OnClick()
	if (lastCommand == "num" and dotPunched == false ) then
		expression = expression .. ".";
		lastCommand = "num";
		updateVisor(expression);
		dotPunched = true;
	elseif ( Text_calc:GetText() == "0" and dotPunched == false ) then
		expression = "0.";
		lastCommand = "num";
		updateVisor(expression);
		dotPunched = true;
	elseif ( lastCommand == "op" and dotPunched == false ) then
		expression = expression .. "0.";
		lastCommand = "num";
		updateVisor(expression);
		dotPunched = true;	
	end
end

function ButtonEq_OnClick()
	if (expression ~= "" and checkDivZero() == true) then
		if (lastCommand == "op" ) then
			expression = string.sub(expression,1,string.len(expression)-1);	
			RunScript('solve("'.. expression ..' = " .. ' .. expression .. ')');
			Text_calc:SetText(expression);
		else
			RunScript('solve("'.. expression ..' = " .. ' .. expression .. ')');
			Text_calc:SetText(expression);
		end
		lastCommand = "num";
	end
end

function checkDivZero()
	if ( lastOp == "/" and lastDivIdx < string.len(expression) ) then
		rightOperand = tonumber(string.sub(expression,lastDivIdx+1,string.len(expression)));
		if ( rightOperand == 0 ) then
			updateVisor("Cannot divide by 0. Reset.");
			lastCommand = "num";
			dotPunched = false;
			expression = "0";
			lastOp = "";
			lastDivIdx = 0;
			return false;
		end
	end
	return true;			
end

function numClick(num)
	if ( Text_calc:GetText() == "0" ) then
		expression = num;
	else
		expression = expression .. num;
	end
	lastCommand = "num";
	updateVisor(expression);
end

function opClick(op)
	if (lastCommand == "num" or Text_calc:GetText() == "0" ) then
		expression = expression .. op;
	else
		expression = string.sub(expression,1,string.len(expression)-1);
		expression = expression .. op;
	end
	if ( op == "/" ) then
		lastDivIdx = string.len(expression);
	end
	lastCommand = "op";
	lastOp = op;
	dotPunched = false;
	updateVisor(expression);
end

function ButtonClear_OnClick()
	clear();
end

function solve(ans)
	expression = string.sub(ans,string.len(expression)+4,string.len(ans));
	down = tonumber(expression);
	downFloor = math.floor(down);
	if ( down ~= downFloor ) then
		dotPunched = true;
	end
	Text_calc:SetText(ans);
end

function clear()
	Text_calc:SetText("0");
	lastCommand = "num";
	dotPunched = false;
	expression = "0";
	lastOp = "";
	lastDivIdx = 0;
end

function updateVisor(str)
	Text_calc:SetText(str);
end
